﻿using System;
using System.Collections.Generic;
using System.Text;

namespace IceDog.SmallProject.ImageHexStringConvert
{
    public static class ImgTxtConvert
    {
        /// <summary>
        /// 转换为十六进制字符串
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string ConvertToHexString(byte[] bytes)
        {
            var builder = new StringBuilder();
            for (int i = 0; i < bytes.Length; i++)
            {
                var b = bytes[i];
                var c = Convert.ToString(b, 16);
                c = c.Length <= 1 ? "0" + c : c;
                builder.Append(c);
            }
            return builder.ToString();
        }

        /// <summary>
        /// 转换为字节
        /// </summary>
        /// <param name="hex"></param>
        /// <returns></returns>
        public static byte[] ConvertToBytes(string hex)
        {
            var inputByteArray = new byte[hex.Length / 2];
            for (var x = 0; x < inputByteArray.Length; x++)
            {
                var i = Convert.ToInt32(hex.Substring(x * 2, 2), 16);
                inputByteArray[x] = (byte)i;
            }
            return inputByteArray;
        }
    }
}
